package web.User;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/api/setUpload")
@MultipartConfig
public class SetUpload extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取Part对象
        Part part = req.getPart("img");

        //响应日期
        //Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        //2、指定一个保存的本地路径
        String fileName = part.getSubmittedFileName();
        //获取项目部署的路径
        String path = "D:/myProject/vue2+elementui电商管理系统/commercexitong/src/assets/img/";
        //4、保存
        part.write(path+fileName);
        //5、响应一个图片地址
        resp.getWriter().println(fileName);//图片访问的网络
    }
}
